
//  QYGNavigationController.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/1.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGNavigationController.h"

@interface QYGNavigationController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

//@property (strong, nonatomic)  UIPanGestureRecognizer *pan;
@end

@implementation QYGNavigationController
+ (void)initialize{
    if (self == [QYGNavigationController class]) {
        
        UINavigationBar *bar = [UINavigationBar appearance];
        
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor clearColor];
        
        bar.barTintColor = [UIColor qyg_mainColor];
        bar.tintColor = [UIColor whiteColor];
        bar.titleTextAttributes = @{
                                     NSForegroundColorAttributeName : [UIColor whiteColor],
                                     NSFontAttributeName : ScaleBoldFont(17),
                                     NSShadowAttributeName :shadow
                                     };
        
        [bar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        [bar setShadowImage:[[UIImage alloc] init]];
        [bar setBackIndicatorImage:[UIImage imageNamed:@"back"]];
        //设置item
        UIBarButtonItem *item = [UIBarButtonItem appearance];
        NSDictionary *attributes = @{NSFontAttributeName:ScaleFont(12),
                                     NSForegroundColorAttributeName:[UIColor whiteColor],
                                     NSShadowAttributeName : shadow
                                     };
        [item setTitleTextAttributes:attributes forState:UIControlStateNormal];
        
        
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.interactivePopGestureRecognizer.delegate = self;
    self.navigationBar.translucent = NO;
    //1.禁用系统自带的返回手势
//    self.interactivePopGestureRecognizer.enabled = NO;
//    //2.捕获返回手势的代理
//    id target = self.interactivePopGestureRecognizer.delegate;
//    // 3.添加全屏滑动返回手势，事件指向系统自带的Pop转场
//    SEL backGestureSelector = NSSelectorFromString(@"handleNavigationTransition:");
//    self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:backGestureSelector];
//    //4.添加手势
////    [self.view addGestureRecognizer:self.pan];
//
//    //5.设置自己为自己代理，这样的设置不科学，但可用
//    self.delegate = self;
    
}

 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
 {
     if (self.childViewControllers.count > 0) { // 非根控制器
     
         UIImage * backImage = [UIImage imageNamed:@"back"];
         UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
         
         viewController.hidesBottomBarWhenPushed = YES;
     
         viewController.navigationItem.leftBarButtonItem = item;
     }
     self.navigationBarHidden = NO;
     [super pushViewController:viewController animated:animated];
 }


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.childViewControllers.count>1;
}
////导航控制器代理
//-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    // 判断下当前控制器是不是根控制器
//    if (navigationController.viewControllers.count >1) {
//        self.pan.enabled = YES;
//    }else{
//        self.pan.enabled = NO;//手势无效，无法转场，自然不会出现转场错乱的问题。
//    }
//}

 - (void)back
 {
     [self popViewControllerAnimated:YES];
 }
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
